package com.yzy.controller.basic;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yzy.domain.entity.annotation.PassToken;
import com.yzy.domain.vo.system.LoginAccountVo;
import com.yzy.domain.entity.system.Account;
import com.yzy.domain.entity.system.AccountPermission;
import com.yzy.mapper.system.AccountMapper;
import com.yzy.mapper.system.AccountPermissionMapper;
import com.yzy.service.AccountService;
import com.yzy.util.response.ResponseUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.UUID;

/**
 * 
 */
@Api(tags = {"用户相关接口"})
@RequestMapping("/account")
@RestController
public class AccountController {

    private final AccountService accountService;

    @Resource
    private AccountMapper accountMapper;

    @Resource
    private AccountPermissionMapper accountPermissionMapper;

    public AccountController(AccountService accountService) {
        this.accountService = accountService;
    }

    @PassToken
    @PostMapping(value = "/login-username")
    public ResponseEntity<?> loginByUserName(HttpServletRequest request, @RequestBody LoginAccountVo loginAccount, HttpServletResponse response) {
        String token = accountService.loginByUserName(request, loginAccount.getUserName(), loginAccount.getPassword(), response);
        return ResponseUtils.OK(token);
    }

    @PassToken
    @ApiOperation(value = "增【人员-暂未使用】")
    @PostMapping("/create")
    public ResponseEntity<?> create(@RequestBody Account account) {
        account.setId(UUID.randomUUID().toString().replace("-", ""));
        int create = accountMapper.insert(account);
        return ResponseUtils.OK(create > 0 ? account : "人员保存失败");
    }

    @PassToken
    @ApiOperation(value = "删【人员】")
    @DeleteMapping("/delete")
    public ResponseEntity<?> delete(@RequestBody Account account) {
        Account select = accountMapper.selectById(account.getId());
        int delete = accountMapper.deleteById(account.getId());
        // 删除映射权限信息
        LambdaQueryWrapper<AccountPermission> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AccountPermission::getAccountId, account.getId());
        accountPermissionMapper.delete(wrapper);
        return ResponseUtils.OK(delete > 0 ? select : "人员删除失败");
    }

    @PassToken
    @ApiOperation(value = "改【人员-暂未使用】")
    @PutMapping("/update")
    public ResponseEntity<?> update(@RequestBody Account account) {
        return ResponseUtils.OK(accountMapper.updateById(account) > 0 ? account : "设备配置修改失败");
    }

    // @PassToken
    @ApiOperation(value = "查【人员】-条件和分页")
    @GetMapping("/retrieve")
    public ResponseEntity<?> retrieve(@RequestParam @ApiParam(value = "页码") Integer pageNum, @RequestParam Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        LambdaQueryWrapper<Account> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Account::getCreatedOn);
        List<Account> list = accountMapper.selectList(wrapper);
        return ResponseUtils.OK(new PageInfo<>(list));
    }

    @PassToken
    @ApiOperation(value = "查【人员-暂未使用】-按人员id")
    @GetMapping("/retrieveAccountById")
    public ResponseEntity<?> retrieveDeviceById(@RequestParam String id) {
        return ResponseUtils.OK(accountMapper.selectById(id));
    }

}
